package com.aplink.generic.pojo;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONObject;

import com.aplink.generic.dictionary.GenericDictionary;

public class ContentData extends GenericDictionary<String, ObjPropInfo>
        implements Serializable {
    private static final long serialVersionUID = -511279188350326409L;
    private final ArrayList<ObjPropInfo> mCreatedTimeObject;
    private ArrayList<String> mDataRules;
    private final ArrayList<ObjPropInfo> mForiegnKeys;
    private final StringBuilder mGroupBy;
    private ObjPropInfo mImageLocalPath;
    private ObjPropInfo mImageServerPath;
    private final StringBuilder mJoins;
    private final ArrayList<String> mJoinTables;
    private ObjPropInfo mLatitudeCol;
    private ObjPropInfo mLongitudeCol;
    private String mParentTable;
    private final ArrayList<ObjPropInfo> mPrimaryKeys;
    private String mSelectQueryStatements;
    private StringBuilder mSortASCColumn;
    private StringBuilder mSortDSECColumn;
    private final ArrayList<ObjPropInfo> mSyncStatus;
    private String mWebserviceFunction;
    private final GenericDictionary<String, JSONObject> mWebServiceParameters;
    private ArrayList<String> queriedColumns = new ArrayList<String>();
    private ArrayList<ObjPropInfo> selectedColumn = new ArrayList<ObjPropInfo>();

    public ContentData() {
        this.mPrimaryKeys = new ArrayList<ObjPropInfo>();
        this.mCreatedTimeObject = new ArrayList<ObjPropInfo>();
        this.mImageServerPath = new ObjPropInfo();
        this.mImageLocalPath = new ObjPropInfo();
        this.mSortASCColumn = new StringBuilder();
        this.mJoins = new StringBuilder();
        this.mSortDSECColumn = new StringBuilder();
        this.mSyncStatus = new ArrayList<ObjPropInfo>();
        this.mGroupBy = new StringBuilder();
        this.mJoinTables = new ArrayList<String>();
        this.mWebServiceParameters = new GenericDictionary<String, JSONObject>();
        this.mForiegnKeys = new ArrayList<ObjPropInfo>();
        setDataRules(new ArrayList<String>());
    }

    public void addASCColumn(final String columnName) {
        if (this.mSortASCColumn.length() != 0) {
            this.mSortASCColumn.append(" , ");
        }
        this.mSortASCColumn.append(columnName);
    }

    public void addCreatedTimeObject(final ObjPropInfo createdTimeObject) {
        this.mCreatedTimeObject.add(createdTimeObject);
    }

    public void addDESCColumn(final String columnName) {
        if (this.mSortDSECColumn.length() != 0) {
            this.mSortDSECColumn.append(" , ");
        }
        this.mSortDSECColumn.append(columnName);
    }

    public void addForiegnKeys(final ObjPropInfo primaryKey) {
        this.mForiegnKeys.add(primaryKey);
    }

    public void addGroupBy(final String groupBy) {
        if (this.mGroupBy.length() != 0) {
            this.mGroupBy.append(" , ");
        }
        this.mGroupBy.append(groupBy);
    }

    public void addJoin(String leftTable, String joinType,
            final String rightTable, final String onLeftCondition,
            final String onRightCondition) {
        if ((leftTable == null) || (this.mJoins.length() != 0)) {
            leftTable = "";
        } else {
            if (!this.mJoinTables.contains(leftTable)) {
                this.mJoinTables.add(leftTable);
            }
        }
        if (!this.mJoinTables.contains(rightTable)) {
            this.mJoinTables.add(rightTable);
        }
        if (joinType.equalsIgnoreCase("RIGHT JOIN")) {
            joinType = "LEFT JOIN";
            this.mJoins.append(rightTable).append(" " + joinType + " ")
                    .append(leftTable).append(" ON ").append(onRightCondition)
                    .append("=").append(onLeftCondition);
        } else {
            this.mJoins.append(leftTable).append(" " + joinType + " ")
                    .append(rightTable).append(" ON ").append(onLeftCondition)
                    .append("=").append(onRightCondition);
        }
    }

    public void addPrimaryKeys(final ObjPropInfo primaryKey) {
        this.mPrimaryKeys.add(primaryKey);
    }

    public void addSyncStatus(final ObjPropInfo syncStatus) {
        this.mSyncStatus.add(syncStatus);
    }

    public boolean contains(final ObjPropInfo objPropInfo) {
        String key = null;
        if (objPropInfo.isFKey() && (objPropInfo.getForeignTable() != null)) {
            key = objPropInfo.getForeignFullColumnName();
        } else {
            key = objPropInfo.getFullColumnName();
        }
        return containsKey(key);
    }

    public ObjPropInfo getCreatedTime(final String table) {
        for (final ObjPropInfo propInfo : this.mCreatedTimeObject) {
            if (propInfo.getTable().equalsIgnoreCase(table)) {
                return propInfo;
            }
        }
        return null;
    }

    public ArrayList<ObjPropInfo> getCreatedTimeObject() {
        return this.mCreatedTimeObject;
    }

    public ArrayList<String> getDataRules() {
        return this.mDataRules;
    }

    public ArrayList<ObjPropInfo> getForiegnKeys() {
        return this.mForiegnKeys;
    }

    public String getGroupBy() {
        return this.mGroupBy.toString();
    }

    public ObjPropInfo getImageLocalPath() {
        return this.mImageLocalPath;
    }

    public ObjPropInfo getImageServerPath() {
        return this.mImageServerPath;
    }

    public String getJoins() {
        return this.mJoins.toString();
    }

    public final ObjPropInfo getLatitudeCol() {
        return mLatitudeCol;
    }

    public final ObjPropInfo getLongitudeCol() {
        return mLongitudeCol;
    }

    public String getMainTable() {
        return this.mParentTable;
    }

    public String getOderBy() {
        String groupBy = null;
        if (this.mSortASCColumn.length() != 0) {
            groupBy = this.mSortASCColumn.toString() + " ASC";
        }
        if (this.mSortDSECColumn.length() != 0) {
            if (groupBy != null) {
                groupBy += "," + this.mSortDSECColumn.toString() + " DESC";
            } else {
                groupBy = this.mSortDSECColumn.toString() + " DESC";
            }
        }

        return groupBy;
    }

    public ObjPropInfo getPrimaryKey(final String table) {
        for (final ObjPropInfo propInfo : this.mPrimaryKeys) {
            if (propInfo.getTable().equalsIgnoreCase(table)) {
                return propInfo;
            }
        }
        return null;
    }

    public ArrayList<ObjPropInfo> getPrimaryKeys() {
        return this.mPrimaryKeys;
    }

    public ArrayList<String> getQueriedColumns() {
        return this.queriedColumns;
    }

    public ArrayList<ObjPropInfo> getSelectedColumn() {
        return this.selectedColumn;
    }

    public String getSelectQueryStatements() {
        return this.mSelectQueryStatements;
    }

    public String getSortASCColumn() {
        return this.mSortASCColumn.toString();
    }

    public String getSortDSECColumn() {
        return this.mSortDSECColumn.toString();
    }

    public ArrayList<ObjPropInfo> getSyncStatus() {
        return this.mSyncStatus;
    }

    public ObjPropInfo getSyncStatus(final String table) {
        for (final ObjPropInfo propInfo : this.mSyncStatus) {
            if (propInfo.getTable().equalsIgnoreCase(table)) {
                return propInfo;
            }
        }
        return null;
    }

    public String getWebserviceFunction() {
        return this.mWebserviceFunction;
    }

    public GenericDictionary<String, JSONObject> getWebServiceParameters() {
        return this.mWebServiceParameters;
    }

    public boolean hasImage() {
        return (this.mImageLocalPath != null)
                || (this.mImageServerPath != null);
    }

    public boolean isContainTable(final String table) {
        return this.mJoinTables.contains(table);
    }

    public boolean isHasCreatedTime() {
        return this.mCreatedTimeObject.size() > 0;
    }

    public void setDataRules(final ArrayList<String> dataRules) {
        this.mDataRules = dataRules;
    }

    public void setImageLocalPath(final ObjPropInfo imageLocalPath) {
        this.mImageLocalPath = imageLocalPath;
    }

    public void setImageServerPath(final ObjPropInfo imageServerPath) {
        this.mImageServerPath = imageServerPath;
    }

    public final void setLatitudeCol(final ObjPropInfo latitudeCol) {
        mLatitudeCol = latitudeCol;
    }

    public final void setLongitudeCol(final ObjPropInfo longitudeCol) {
        mLongitudeCol = longitudeCol;
    }

    public void setMainTable(final String parentTable) {
        this.mParentTable = parentTable;
    }

    public void setQueriedColumns(final ArrayList<String> queriedColumns) {
        this.queriedColumns = queriedColumns;
    }

    public void setSelectedColumn(final ArrayList<ObjPropInfo> selectedColumn) {
        this.selectedColumn = selectedColumn;
    }

    public void setSelectQueryStatements(final String selectQueryStatements) {
        this.mSelectQueryStatements = selectQueryStatements;
    }

    public void setSortASCColumn(final StringBuilder sortASCColumn) {
        this.mSortASCColumn = sortASCColumn;
    }

    public void setSortDSECColumn(final StringBuilder sortDSECColumn) {
        this.mSortDSECColumn = sortDSECColumn;
    }

    public void setWebserviceFunction(final String webserviceFunction) {
        this.mWebserviceFunction = webserviceFunction;
    }
}
